﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;

namespace myMvcBlog.Infrastructure
{
    public class MyObject
    {
        public static string Convert(object obj)
        {
            return obj.ToString();
        }
    }

    //public static class ListExtensions
    //{
    //    public static IList<T> ToList<T>(this IEnumerable<dynamic> dynamics) where T : class
    //    {
    //        List<T> lists = new List<T>();
    //        var properties = typeof(T).GetProperties();

    //        foreach (var dynamic in dynamics)
    //        {
    //            var item = (IDictionary<string, object>)dynamic;

    //            List<MemberBinding> bindings = new List<MemberBinding>();
    //            foreach (var property in properties)
    //            {
    //                MethodCallExpression propertyValue = null;

    //                if (property.PropertyType == typeof(string))
    //                {
    //                    propertyValue = Expression.Call(
    //                                 typeof(MyObject).GetMethod("Convert"),
    //                                 Expression.Constant(item[property.Name]));
    //                }
    //                else
    //                {
    //                    propertyValue = Expression.Call(
    //                                    property.PropertyType.GetMethod("Parse", new Type[] { typeof(string) }),
    //                                    Expression.Constant(item[property.Name].ToString()));
    //                }
    //                MemberBinding binding = Expression.Bind(property, propertyValue);
    //                bindings.Add(binding);
    //            }

    //            Expression initializer = Expression.MemberInit(Expression.New(typeof(T)), bindings);
    //            Expression<Func<T>> lambda = Expression.Lambda<Func<T>>(initializer);

    //            lists.Add(lambda.Compile().Invoke());
    //        }

    //        return lists;

    //    }
    //}

   
}